<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
       <el-form-item label="序号" prop="sequenceNumber">
        <el-input v-model="formData.sequenceNumber" placeholder="请输入序号" />
      </el-form-item>
      <el-form-item label="物料ID" prop="materialId">
        <el-input v-model="formData.materialId" placeholder="请输入物料ID" />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="formData.materialName" placeholder="请输入物料名称" />
      </el-form-item>
      <el-form-item label="物料编号" prop="materialCode">
        <el-input v-model="formData.materialCode" placeholder="请输入物料编号" />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input v-model="formData.unitPrice" placeholder="请输入单价" />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input v-model="formData.specification" placeholder="请输入规格" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="formData.unit" placeholder="请输入单位" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input v-model="formData.model" placeholder="请输入型号" />
      </el-form-item>
      <el-form-item label="计算公式" prop="calculationFormula">
        <el-input v-model="formData.calculationFormula" placeholder="请输入计算公式" />
      </el-form-item>
      <el-form-item label="计算方式" prop="calculationMethod">
        <el-input v-model="formData.calculationMethod" placeholder="请输入计算方式" />
      </el-form-item>
      <el-form-item label="公式说明" prop="formulaDescription">
        <Editor v-model="formData.formulaDescription" height="150px" />
      </el-form-item>
      <el-form-item label="备注1" prop="remark1">
        <el-input v-model="formData.remark1" placeholder="请输入备注1" />
      </el-form-item>
      <el-form-item label="备注2" prop="remark2">
        <el-input v-model="formData.remark2" placeholder="请输入备注2" />
      </el-form-item>
      <el-form-item label="备注3" prop="remark3">
        <el-input v-model="formData.remark3" placeholder="请输入备注3" />
      </el-form-item>
      <el-form-item label="备注4" prop="remark4">
        <el-input v-model="formData.remark4" placeholder="请输入备注4" />
      </el-form-item>
      <el-form-item label="备注5" prop="remark5">
        <el-input v-model="formData.remark5" placeholder="请输入备注5" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { PurAccessoryMaterialQuotationApi } from '@/api/erp/puraccessorymaterialquotation'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  quotationId: undefined,
  sequenceNumber: undefined,
  materialId: undefined,
  materialName: undefined,
  materialCode: undefined,
  unitPrice: undefined,
  specification: undefined,
  unit: undefined,
  remark: undefined,
  model: undefined,
  calculationFormula: undefined,
  calculationMethod: undefined,
  formulaDescription: undefined,
  remark1: undefined,
  remark2: undefined,
  remark3: undefined,
  remark4: undefined,
  remark5: undefined
})
const formRules = reactive({
  quotationId: [{ required: true, message: '报价单ID不能为空', trigger: 'blur' }],
  sequenceNumber: [{ required: true, message: '序号不能为空', trigger: 'blur' }],
  materialId: [{ required: true, message: '物料ID不能为空', trigger: 'blur' }],
  materialName: [{ required: true, message: '物料名称不能为空', trigger: 'blur' }],
  materialCode: [{ required: true, message: '物料编号不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, quotationId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.quotationId = quotationId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PurAccessoryMaterialQuotationApi.getPurAccessoryMaterialQuotationDetail(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await PurAccessoryMaterialQuotationApi.createPurAccessoryMaterialQuotationDetail(data)
      message.success(t('common.createSuccess'))
    } else {
      await PurAccessoryMaterialQuotationApi.updatePurAccessoryMaterialQuotationDetail(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    quotationId: undefined,
    sequenceNumber: undefined,
    materialId: undefined,
    materialName: undefined,
    materialCode: undefined,
    unitPrice: undefined,
    specification: undefined,
    unit: undefined,
    remark: undefined,
    model: undefined,
    calculationFormula: undefined,
    calculationMethod: undefined,
    formulaDescription: undefined,
    remark1: undefined,
    remark2: undefined,
    remark3: undefined,
    remark4: undefined,
    remark5: undefined
  }
  formRef.value?.resetFields()
}
</script>